package com.net.example;
import java.io.IOException;

import jpcap.*;
import jpcap.packet.EthernetPacket;
import jpcap.packet.ICMPPacket;
import jpcap.packet.IPPacket;
import jpcap.packet.Packet;
import jpcap.packet.TCPPacket;
import jpcap.packet.UDPPacket;

public class Capture {

    public static void main(String[] args)
    {
        /*--------------    第一步绑定网络设备       --------------*/
        NetworkInterface[] devices = JpcapCaptor.getDeviceList();

        for(NetworkInterface n : devices)
        {
            System.out.println(n.name + "     |     " + n.description + "     |     " + new String(n.mac_address));
            String value = "";
            for(int i = 0;i < n.mac_address.length; i++){
             String sTemp = Integer.toHexString(0xFF &  n.mac_address[i]);
             value = value+sTemp+":";
            }
              
            value = value.substring(0,value.lastIndexOf(":"));
            System.out.println("value="+value);
        }
        System.out.println("-------------------------------------------");

        JpcapCaptor jpcap = null;
        int caplen = 1512;
        boolean promiscCheck = true;

        try{
            jpcap = JpcapCaptor.openDevice(devices[2], caplen, promiscCheck, 50);
        }catch(IOException e)
        {
            e.printStackTrace();
        }

        /*----------第二步抓包-----------------*/
        int i = 0;
        while(true)
        {
            Packet packet  = jpcap.getPacket();
            
            if (packet instanceof TCPPacket){
            	System.out.println("TCP 数据包");
            	TCPPacket tcp = (TCPPacket) packet;
            	System.out.println("源端口:" + tcp.src_port);
            	System.out.println("目的端口:" + tcp.dst_port);
            	
            } 
            
            if (packet instanceof ICMPPacket){
            	ICMPPacket tcp = (ICMPPacket) packet;
            	System.out.println("419276cdf9dcee37519005204cfa6bd524e4e82f0129786f:db22d50d0c58de90054888d93fabc486efede7ff51133c22".length());;
            }
            
            if (packet instanceof UDPPacket){
            	System.out.println("TCP 数据包");
            	UDPPacket tcp = (UDPPacket) packet;
            	System.out.println("源端口:" + tcp.src_port);
            	System.out.println("目的端口:" + tcp.dst_port);
            	
            }
            
            if (packet instanceof UDPPacket){
            	System.out.println("TCP 数据包");
            	UDPPacket tcp = (UDPPacket) packet;
            	System.out.println("源端口:" + tcp.src_port);
            	System.out.println("目的端口:" + tcp.dst_port);
            	
            }
            
            if (packet instanceof UDPPacket){
            	System.out.println("TCP 数据包");
            	UDPPacket tcp = (UDPPacket) packet;
            	System.out.println("源端口:" + tcp.src_port);
            	System.out.println("目的端口:" + tcp.dst_port);
            	
            }
            
            if (packet instanceof UDPPacket){
            	System.out.println("TCP 数据包");
            	UDPPacket tcp = (UDPPacket) packet;
            	System.out.println("源端口:" + tcp.src_port);
            	System.out.println("目的端口:" + tcp.dst_port);
            	
            }
            
            
            
            
            
            
            if(packet instanceof IPPacket && ( (IPPacket) packet ).version == 4 )
            {
                i++;
                IPPacket ipPacket = (IPPacket)packet;

                System.out.println("版本：IPv4");
/*                System.out.println("优先权：" + ipPacket.priority);
                System.out.println("区分服务：最大的吞吐量： " + ipPacket.t_flag);
                System.out.println("区分服务：最高的可靠性：" + ipPacket.r_flag);
                System.out.println("长度：" + ipPacket.length);
                System.out.println("标识：" + ipPacket.ident);
                System.out.println("DF:Don't Fragment: " + ipPacket.dont_frag);
                System.out.println("NF:Nore Fragment: " + ipPacket.more_frag);
                System.out.println("片偏移：" + ipPacket.offset);
                System.out.println("生存时间："+ ipPacket.hop_limit);*/

                String protocol ="";
                switch(new Integer(ipPacket.protocol))
                {
                    case 1:protocol = "ICMP";break;
                    case 2:protocol = "IGMP";break;
                    case 6:protocol = "TCP";break;
                    case 8:protocol = "EGP";break;
                    case 9:protocol = "IGP";break;
                    case 17:protocol = "UDP";break;
                    case 41:protocol = "IPv6";break;
                    case 89:protocol = "OSPF";break;
                    default : break;
                }
                System.out.println("协议：" + protocol);
                System.out.println("源IP " + ipPacket.src_ip.getHostAddress());
                System.out.println("目的IP " + ipPacket.dst_ip.getHostAddress());
//                System.out.println("域名 " + ip.dst_ip.getCanonicalHostName());
                //MAC地址
                EthernetPacket ethPacket = (EthernetPacket) ipPacket.datalink;
                System.out.println("源MAC地址: " + ethPacket.getSourceAddress());
                System.out.println("目的MAC地址: " + ethPacket.getDestinationAddress());
                
                //IP数据包长度
                
                //源端口
                
                
        /*        System.out.println("源主机名: " + ipPacket.src_ip);
                System.out.println("目的主机名: " + ipPacket.dst_ip);*/
                
                
                
                
                System.out.println("----------------------------------------------");
                
//                byte[] src_ip = ip.src_ip.getAddress();
//                for(int j = 0; j < 4; j ++){
//                	System.out.print(src_ip[j]);
//                }
                System.out.println();
            }
        }
    }

}
